package com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonCodeEnum;
import com.xyht.sca_s.student_manage_system.common.commonReturn.CommonResult;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.SmsWnsBookFeesPlan;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.SmsWnsBookFeesPlanReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.req.SmsWnsIdListReq;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.entity.resp.SmsWnsBookFeesPlanResp;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.mapper.SmsWnsBookFeesPlanMapper;
import com.xyht.sca_s.student_manage_system.modules.welcomeNewStudent.service.SmsWnsBookFeesPlanService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

import static com.xyht.sca_s.student_manage_system.common.util.ParamCheckUtil.isNullOrEmpty;

/**
 * <p>
 * 书杂费计划表 服务实现类
 * </p>
 *
 * @author XYHT
 * @since 2024-04-08
 */
@Service
public class SmsWnsBookFeesPlanServiceImpl extends ServiceImpl<SmsWnsBookFeesPlanMapper, SmsWnsBookFeesPlan> implements SmsWnsBookFeesPlanService {

    @Override
    public ResponseResult addPlan(SmsWnsBookFeesPlanReq req) {
        if (isNullOrEmpty(req.getName())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        // 查询是否已存在相同名称的书杂费计划
        int count = this.count(new LambdaQueryWrapper<SmsWnsBookFeesPlan>()
                .eq(SmsWnsBookFeesPlan::getName, req.getName()));
        if (count > 0) {
            return CommonResult.failed(CommonCodeEnum.WNS_STUDENT_BOOK_FEES_PLAN_EXIST);
        }

        SmsWnsBookFeesPlan bookFeesPlan = new SmsWnsBookFeesPlan();
        BeanUtils.copyProperties(req, bookFeesPlan);
        this.save(bookFeesPlan);

        return CommonResult.success(bookFeesPlan.getId());
    }

    @Override
    @Transactional
    public ResponseResult delPlan(SmsWnsIdListReq req) {
        List<String> idList = req.getIdList();
        if (idList.isEmpty()) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        this.removeByIds(idList);

        return CommonResult.success();
    }

    @Override
    public ResponseResult updatePlan(SmsWnsBookFeesPlanReq req) {
        if (isNullOrEmpty(req.getId())) {
            return CommonResult.failed(CommonCodeEnum.INVALID_PARAM);
        }

        // 校验书杂费计划是否存在
        SmsWnsBookFeesPlan bookFeesPlan = this.getById(req.getId());
        if (bookFeesPlan == null) {
            return CommonResult.failed(CommonCodeEnum.WNS_STUDENT_BOOK_FEES_PLAN_NOT_EXIST);
        }

        BeanUtils.copyProperties(req, bookFeesPlan);
        this.updateById(bookFeesPlan);

        return CommonResult.success();
    }

    @Override
    public ResponseResult listPlan(Integer pageNum, Integer pageSize, String searchStr) {
        LambdaQueryWrapper<SmsWnsBookFeesPlan> queryWrapper = new LambdaQueryWrapper<SmsWnsBookFeesPlan>()
                .orderByDesc(SmsWnsBookFeesPlan::getCreateTime);

        if (!isNullOrEmpty(searchStr)) {
            queryWrapper.and(wrapper -> wrapper.like(SmsWnsBookFeesPlan::getName, searchStr).or()
                    .like(SmsWnsBookFeesPlan::getDescription, searchStr));
        }

        // 分页查询
        Page<SmsWnsBookFeesPlan> page = new Page<>(pageNum, pageSize);
        this.page(page, queryWrapper);
        List<SmsWnsBookFeesPlanResp> collect = page.getRecords().stream().map(bookFeesPlan -> {
            SmsWnsBookFeesPlanResp planResp = new SmsWnsBookFeesPlanResp();
            BeanUtils.copyProperties(bookFeesPlan, planResp);
            return planResp;
        }).collect(Collectors.toList());

        return CommonResult.success(collect, (int) page.getTotal());
    }
}
